package com.pgytesting.secondhandcar.controller.mcpcar;

import com.pgytesting.secondhandcar.model.OrderName;
import com.pgytesting.secondhandcar.model.PhotographingMaterial;
import com.pgytesting.secondhandcar.model.ServiceInformation;
import com.pgytesting.secondhandcar.model.VehicleInformation;
import com.pgytesting.secondhandcar.service.PhotographingMaterialService;
import com.pgytesting.secondhandcar.service.ServiceInformationService;
import com.pgytesting.secondhandcar.service.VehicleInformationService;
import com.pgytesting.secondhandcar.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author : frosty0804@gmail.com
 * @version : v1.0
 * @projectName : secondhandcar
 * @className : VehicleInfController
 * @description : TODO(车辆控制层)
 * @date : 2019/8/19 11:00
 */
@Slf4j
@RestController
@RequestMapping("/sales/vehicle")
public class VehicleInfController {


    @Autowired
    VehicleInformationService informationService;

    @Autowired
    ServiceInformationService serviceInformationService;

    @Autowired
    PhotographingMaterialService photographingMaterialService;
    
    @Autowired
    VehicleInformationService vehicleInformationService;

    /**
     * 销售补充车辆信息
     *
     * @param vehicleInformation 参数
     * @return
     */
    @RequestMapping(value = "/addVehicle", method = RequestMethod.POST)
    public ResponseMessage insertVehicleInf(@RequestBody VehicleInformation vehicleInformation) {
        try {
            return informationService.update(vehicleInformation);
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }

    /**
     * 补充车辆服务信息
     *
     * @param serviceInformation 参数
     * @param bindingResult      参数校验
     * @return
     */
    @RequestMapping(value = "/updateVehicle", method = RequestMethod.POST)
    public ResponseMessage update(@Valid @RequestBody ServiceInformation serviceInformation, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseMessage.errorResponse(msg);
        }
        try {
            return serviceInformationService.insert(serviceInformation);
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }

    /**
     * 销售上传合影材料
     *
     * @param photographingMaterial 参数
     * @param bindingResult         参数校验
     * @return
     */
    @RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
    public ResponseMessage UploadGroupPhoto(@Valid @RequestBody PhotographingMaterial photographingMaterial, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String msg = bindingResult.getFieldError().getDefaultMessage();
            return ResponseMessage.errorResponse(msg);
        }
        try {
            return photographingMaterialService.insert(photographingMaterial);
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }
    
    /**
     * 销售获取车辆信息
     *
     * @param 
     * @param 
     * @return
     */
    @RequestMapping(value = "/getVehicleInformation", method = RequestMethod.POST)
    public ResponseMessage getVehicleInformation(@RequestBody OrderName orderName) {
        try {
        	if(orderName.getId() == null) {
        		ResponseMessage.errorResponse("订单id不能为空");
        	}
            return vehicleInformationService.queryVehicleByOrderId(orderName);
        } catch (Exception e) {
            log.error("服务异常", e);
            return ResponseMessage.errorResponse("服务异常");
        }
    }
}
